import { defineStore } from "pinia";
import requests from "../utils/requests";

const useUserInfoStore=defineStore('userInfo',{
	state:()=>({
		popupShow:false,
		form:{
			nickname:'',
			sex:'',
			age:'',
			city:'',
			phone:'',
			email:''
		},
		nickname:'暂无数据',
		sex:'暂无数据',
		age:'暂无数据',
		city:'暂无数据',
		phone:uni.getStorageSync('phone'),
		email:uni.getStorageSync('email')
	}),
	actions:{
		openPopup(){
			this.popupShow=true
		},
		async getUserInfo(){
			const res=await requests({
				url:"/base/userInfo/",
				method:'get',
				data:{
					username:uni.getStorageSync('username')
				}
			})
			if(res.code===200){
				this.nickname=res.data.nickName
				this.age=res.data.age
				this.sex=res.data.sex
				this.city=res.data.city
				this.phone=res.data.username.phone
				this.email=res.data.username.email
			}
		},
		async changeUserInfo(){
			const data={}
			data['username']=uni.getStorageSync('username')
			for(let i in this.form){
				if(this.form[i]!=''){
					data[i]=this.form[i]
				}
			}
			console.log(data)
			const res=await requests({
				url:'/base/userInfo/',
				method:'post',
				data:data
			})
			if(res.code===200){
				this.getUserInfo()
				uni.$u.toast(`修改成功`)
				for(let i in this.form){
					this.form.i=''
				}
			}else{
				uni.$u.toast(`修改失败,${res.error}`)
			}
		}
	}
})
export default useUserInfoStore;